package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    /*
    * 新增菜品
    * */
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish); //属性名一致
        //1、菜品表插入数据
        dishMapper.insert(dish);
        //2、菜品口味表插入n条数据
        List<DishFlavor> list=dishDTO.getFlavors();
        if(list!=null&&list.size()>0){
            //设置菜品id
            for (DishFlavor dishFlavor : list) {
                dishFlavor.setDishId(dish.getId());
            }
            //批量插入
            dishFlavorMapper.insertBatch(list);
        }
    }

    /*
    * 菜品分页查询
    * */

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //配置分页查询
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //执行查询语句
        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        //封装结果
        return new PageResult(page.getTotal(),page.getResult());
    }

    /*
    * 菜品批量删除
    * */
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        //判断菜品是否可以删除
        for (Long id : ids) {
            Dish dish=dishMapper.getByid(id);
            if (dish.getStatus()== StatusConstant.ENABLE){
                //菜品正在售卖，不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断菜品是否关联套餐
        List<Long> setmealIds=setMealDishMapper.getSetmealIdByDishIds(ids);
        if (setmealIds!=null && setmealIds.size()>0){
            //菜品关联套餐，不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
/*        //删除菜品
        for (Long id : ids) {
            dishMapper.deleteById(id);
        }*/
        dishMapper.deleteBatch(ids);
        //删除口味
/*        for(Long id:ids){
            dishFlavorMapper.deleteByDishId(id);
        }*/
        dishFlavorMapper.deleteBatch(ids);
    }

    /*
    * 根据id查询菜品信息以及对应口味信息
    * */

    @Override
    public DishVO selectById(Long id) {
        //根据id查询菜品数据
        Dish dish = dishMapper.getByid(id);
        //根据id查询口味信息
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        //封装对象
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /*
    * 修改菜品信息
    * */
    @Transactional
    @Override
    public void updateWithFlavor(DishDTO dish) {
        //修改菜品信息
        Dish d = new Dish();
        BeanUtils.copyProperties(dish,d);
        dishMapper.update(d);
        //口味非空，删除口味信息
        dishFlavorMapper.deleteByDishId(dish.getId());
        //添加口味信息
        List<DishFlavor> list = dish.getFlavors();
        if(list!=null&&list.size()>0){
            //设置菜品id
            for (DishFlavor dishFlavor : list) {
                dishFlavor.setDishId(dish.getId());
            }
            //批量插入
            dishFlavorMapper.insertBatch(list);
        }
    }
    /*
    * 设置菜品起售或停售
    * */

    @Override
    public void updateStatus(Long id, Integer status) {
        Dish dish=dishMapper.getByid(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    /*
    * 根据菜品类型查询菜品信息
    * */

    @Override
    public List<Dish> getByCategpryID(Dish dish) {
        return dishMapper.list(dish);
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
